package adapter;

import java.util.ArrayList;

import JsonModel.FamiliarFriend;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.adgroup.smartnote.AddMemberToGroupActivity;
import com.adgroup.smartnote.R;
import com.androidquery.AQuery;

public class AdapterChooseFriendToGroup {
	
	private Context context;
	private ArrayList<FamiliarFriend> friends = new ArrayList<FamiliarFriend>();
	private boolean[] check;
	public AdapterChooseFriendToGroup(Context context, ArrayList<FamiliarFriend> friends) {
		this.context = context;
		this.friends = friends;
		check = new boolean[friends.size()];
		for(int i=0;i<check.length;i++){
			check[i] = false;
		}
	}
	public class Adapter extends BaseAdapter {

		@Override
		public int getCount() {
			return friends.size();
		}

		@Override
		public Object getItem(int arg0) {
			return friends.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		public void setFriends(ArrayList<FamiliarFriend> u)
		{
			//friends.clear();
			friends = u;
			check = new boolean[friends.size()];
			for(int i=0;i<check.length;i++){
				check[i] = false;
			}
		}
		public void resetCheckBox()
		{
			for(int i=0;i<check.length;i++){
				check[i] = false;
			}
		}
		public void resetCheckBox(FamiliarFriend f)
		{
			int i;
			for(i=0;i<friends.size();i++){
				if(friends.get(i).getUserId().equalsIgnoreCase(f.getUserId())){
					break;
				}
			}
			if(i!=friends.size())
			check[i] = false;
		}
		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			final Holder holder;
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			if (arg1 == null) {
				holder = new Holder();
				arg1 = inflater.inflate(R.layout.itemchoosefriendtogroup, null);
				holder.iv = (ImageView) arg1
						.findViewById(R.id.imageViewLogoItemChooseFriendToGroup);
				holder.tv = (TextView) arg1
						.findViewById(R.id.textViewNickNameItemChooseFriendToGroup);
				holder.chk = (CheckBox) arg1
						.findViewById(R.id.checkBoxChooseItemChooseFriendToGroup);
				
				arg1.setTag(holder);
			} else {
				holder = (Holder) arg1.getTag();
			}
			AQuery aq = new AQuery(arg1);
			Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
			aq.id(holder.iv).image(friends.get(arg0).getLink(), true,
					true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
			holder.tv.setText(friends.get(arg0).getNickName());
			holder.chk.setChecked(check[arg0]);
			Log.d("log",arg0+":"+check[arg0]);		
			holder.chk.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					check[arg0] = holder.chk.isChecked();
					((AddMemberToGroupActivity)context).updateChoseUser(friends.get(arg0), check[arg0]);
					/*holder.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						
						@Override
						public void onCheckedChanged(CompoundButton compoundButton, boolean chk) {					
							check[arg0] = chk;
							((AddMemberToGroupActivity)context).updateChoseUser(friends.get(arg0), check[arg0]);
						}
					});*/
				}
			});
			
			return arg1;
		}

		public void showCheck() {
			for(int i=0;i<check.length;i++){
				Log.d("log",check[i]+"");
			}
		}

		public void checkChoseUser(ArrayList<FamiliarFriend> arrsChose) {
			if(arrsChose!=null){
				for(int i=0;i<friends.size();i++){
					for(int j=0;j<arrsChose.size();j++){
						if(friends.get(i).getUserId().equalsIgnoreCase(arrsChose.get(j).getUserId())){
							check[i] = true;
							break;
						}
					}
				}
			}
		}
	}

	public class Holder {
		ImageView iv;
		TextView tv;
		CheckBox chk;
		LinearLayout root;
	}
}
